package models;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.PostPersist;
import javax.persistence.Transient;

import play.Play;
import play.db.jpa.Model;
import util.S3PhotoUtil;

@Entity
public class Picture extends Model {
	@Column(nullable=true)
	private String path;
	@Column(nullable=false)
	private String mimeType;
	
	@Transient private byte[] data;
	
	public String getPath() {
		return path;
	}
	
	public void setPath(String path) {
		this.path = path;
	}
	
	public String getURL() {
		return "https://s3.amazonaws.com/" + S3PhotoUtil.BUCKET_NAME + "/" + getPath();
	}
	
	@PostPersist
	private void storeS3() {
		setPath(getId() + getMimeType());
		S3PhotoUtil.storePhoto(this);
	}

	public String getMimeType() {
		return mimeType;
	}

	public void setMimeType(String mimeType) {
		this.mimeType = mimeType;
	}

	public byte[] getData() {
		return data;
	}

	public void setData(byte[] data) {
		this.data = data;
	}
}
